home *** CD-ROM | disk | FTP | other *** search
- {*******************************************************
- 'Electronic' Components
-
- This unit contains the code for the the CustomElectric,
- TConductor, TBattery, TSwitch, TResistor and TLamp
- components. These components comprise a class of system
- modelling components designed as a demo of what you can do
- with the Delphi VCL.
-
- They ARE NOT imtended to fully model electronic systems
- at this time. They would need to be much more sophisticated.
- At a minimum they would need a GetResistance method so
- they could 'sense' the resistance (back pressure if you like)
- of the circuit 'downstream'. Also, a TJunction component
- would be required to allow parallel circuits.
-
- What these components ARE designed for is to demonstrate how
- components can be connected together to create complicated
- systems from simple building blocks.
-
- It has been my experience that this approach drastically
- reduces development time and the frustration of debugging
- complicated code.
-
- These components are freeware. Use them as you please. The
- only thing I ask is you publish as freeware anything you
- develop directly from this code. You can also e-mail me
- any improvements you make on these components.
-
- I am specifically interested in any system to make components
- available to the user at run time from some kind of pallet.
-
- Paul Warren
- HomeGrown Software Development
- (c) 1995 Langley British Columbia.
- (604) 530-9097
- e-mail: hg_soft@uniserve.com
- Home page: http://haven.uniserve.com/~hg_soft
-
- ********************************************************}
-
- unit Electric;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs;
-
- type
- TCustomElectric = class(TComponent) { base class }
- private
- { Private declarations }
- FCurrent: LongInt;
- FPotential: LongInt;
- FOutPipe: TCustomElectric;
- FAfterProcess: TNotifyEvent;
- procedure SetCurrent(Value: LongInt);
- procedure SetPotential(Value: LongInt);
- procedure SetAfterProcess(Value: TNotifyEvent);
- procedure Engine; virtual;
- protected
- { Protected declarations }
- procedure After; dynamic;
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- procedure Run; virtual; abstract;
- property OutPipe: TCustomElectric read FOutPipe write FOutPipe;
- published
- { Published declarations }
- property Current: LongInt read FCurrent write SetCurrent default 0;
- property Potential: LongInt read Fpotential write SetPotential default 0;
- property AfterProcess: TNotifyEvent read FAfterProcess write SetAfterProcess;
- end;
-
- TConductor = class(TCustomElectric)
- private
- { Private declarations }
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- procedure Run; override;
- published
- { Published declarations }
- property OutPipe;
- end;
-
- TBattery = class(TConductor)
- private
- { Private declarations }
- FLifetime: integer;
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- procedure Run; override;
- published
- { Published declarations }
- property OutPipe;
- property Lifetime: integer read FLifetime write FLifetime default 100;
- end;
-
- TSwitch = class(TConductor)
- private
- { Private declarations }
- FEnabled: Boolean;
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure Run; override;
- published
- { Published declarations }
- property OutPipe;
- property Enabled: Boolean read FEnabled write FEnabled default false;
- end;
-
- TResistor = class(TConductor)
- private
- { Private declarations }
- FResistance: LongInt;
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- procedure Engine; override;
- procedure Run; override;
- published
- { Published declarations }
- property OutPipe;
- property Resistance: LongInt read FResistance write FResistance default 20;
- end;
-
- TLamp = class(TConductor)
- private
- { Private declarations }
- FLampOn: boolean;
- FRating: byte;
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- procedure Engine; override;
- procedure Run; override;
- published
- { Published declarations }
- property OutPipe;
- property LampOn: boolean read FLampOn write FLampOn default false;
- property Rating: byte read FRating write FRating default 5;
- end;
-
- procedure Register;
-
- implementation
-
- { base class }
- constructor TCustomElectric.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FOutPipe := nil;
- end;
-
- procedure TCustomElectric.SetCurrent(Value: LongInt);
- begin
- FCurrent := Value;
- end;
-
- procedure TCustomElectric.SetPotential(Value: LongInt);
- begin
- FPotential := Value;
- end;
-
- procedure TCustomElectric.SetAfterProcess(Value: TNotifyEvent);
- begin
- FAfterProcess := Value;
- end;
-
- procedure TCustomElectric.After;
- begin
- if Assigned(FAfterProcess) then FAfterProcess(Self);
- end;
-
- procedure TCustomElectric.Engine;
- begin
- After;
- end;
-
- constructor TConductor.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FOutPipe := nil;
- end;
-
- procedure TConductor.Run;
- begin
- if not (OutPipe is TBattery) then
- begin
- Engine;
- OutPipe.Current := Current;
- OutPipe.Potential := Potential;
- OutPipe.Run;
- end;
- end;
-
- constructor TBattery.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FLifetime := 100;
- end;
-
- procedure TBattery.Run;
- begin
- if not (OutPipe is TBattery) then
- begin
- Engine;
- OutPipe.Current := Current;
- OutPipe.Potential := Potential;
- OutPipe.Run;
- end;
- end;
-
- constructor TSwitch.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FEnabled := false;
- end;
-
- destructor TSwitch.Destroy;
- begin
- FEnabled := false;
- inherited Destroy;
- end;
-
- procedure TSwitch.Run;
- begin
- if not (OutPipe is TBattery) and Enabled then
- begin
- Engine;
- OutPipe.Current := Current;
- OutPipe.Potential := Potential;
- OutPipe.Run;
- end else
- begin
- Engine;
- OutPipe.Current := 0;
- OutPipe.Potential := 0;
- OutPipe.Run;
- end;
- end;
-
- constructor TResistor.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FResistance := 20;
- end;
-
- procedure TResistor.Engine;
- begin
- Potential := Potential-(Current*Resistance);
- inherited Engine;
- end;
-
- procedure TResistor.Run;
- begin
- if not (OutPipe is TBattery) then
- begin
- Engine;
- OutPipe.Current := Current;
- OutPipe.Potential := Potential;
- OutPipe.Run;
- end;
- end;
-
- constructor TLamp.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FLampOn := false;
- FRating := 5;
- end;
-
- procedure TLamp.Engine;
- begin
- if Current > Rating then LampOn := true
- else LampOn := false;
- inherited Engine;
- end;
-
- procedure TLamp.Run;
- begin
- if not (OutPipe is TBattery) then
- begin
- Engine;
- OutPipe.Current := Current;
- OutPipe.Potential := Potential;
- OutPipe.Run;
- end;
- end;
-
- procedure Register;
- begin
- RegisterComponents('Misc', [TConductor]);
- RegisterComponents('Misc', [TBattery]);
- RegisterComponents('Misc', [TSwitch]);
- RegisterComponents('Misc', [TResistor]);
- RegisterComponents('Misc', [TLamp]);
- end;
-
- end.
-